package cn.xyfmzmy.bio;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

// bio服务端
// 形象的比喻：serversocket相当于插座，socket相当于插头
public class BioServer {


    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(8080);
        while(true){
            Socket accept = serverSocket.accept();      //阻塞方法
            InputStream inputStream = accept.getInputStream();
            byte[] buffer = new byte[1024];
            int length;
            // bio的本质：只要连接不断开，read不返回-1，就认为链接一直存在，一直阻塞无法向下处理任务
            // bio的性能瓶颈：1个线程只能处理1个任务
            while((length = inputStream.read(buffer)) != -1){
                String message = new String(buffer, 0, length);
                System.out.println(message);
            }
            System.out.println("客户端断开连接");
        }

    }
}
